package com.example.carrierapp.ui.main.goodssource.history

import android.support.v7.widget.LinearLayoutManager
import android.view.View
import com.example.carrierapp.base.MVPBaseFragment
import com.example.carrierapp.bean.RowsBean
import com.example.carrierapp.config.gone
import com.example.carrierapp.config.show
import com.example.carrierapp.ui.adapter.CommonRecyclerAdapter
import com.example.carrierapp.ui.view.recycleview.XRecyclerView
import com.example.carrierapp.viewmodel.itemmodel.ItemGoodsSourceModel
import com.example.lb.fingerdemo.R
import kotlinx.android.synthetic.main.layout_recycleview.*

/**
 * 竞抢记录
 */
class CompetitionHistoryFragment : MVPBaseFragment<CompetitionHistoryContract.View, CompetitionHistoryPresenter>(), CompetitionHistoryContract.View, XRecyclerView.LoadingListener {

    private val adapter by lazy { CommonRecyclerAdapter<ItemGoodsSourceModel>(context, R.layout.item_source_hill) }
    private var isLoadMore = false
    private var page = 0

    override fun createPresenter() = CompetitionHistoryPresenter(context!!)

    override fun getLayout(): Int {
        return R.layout.layout_recycleview
    }

    companion object {
        fun newInstance(): CompetitionHistoryFragment {
            return CompetitionHistoryFragment()
        }
    }

    override fun initData() {
        mPresenter.fetchGoodsSourceList(page)
    }

    override fun initView() {
        recycler_view.layoutManager = LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false)
        recycler_view.setLoadingMoreEnabled(true)
        recycler_view.setPullRefreshEnabled(true)
        recycler_view.setHasFixedSize(true)
        recycler_view.setLoadingListener(this)
        recycler_view.adapter = adapter
    }

    override fun onRefresh() {
        isLoadMore = false
        page = 0
        mPresenter.fetchGoodsSourceList(page)
    }

    override fun onLoadMore() {
        isLoadMore = true
        page++
        mPresenter.fetchGoodsSourceList(page)
    }

    override fun onFetchSuccess(rows: RowsBean<ItemGoodsSourceModel>) {
        rows.items?.let {
            it.forEach {
                it.isDetails = true
            }
            ll_empty.gone()
            if (isLoadMore) {
                recycler_view.loadMoreComplete()
                adapter.insertData(it)
            } else {
                recycler_view.refreshComplete()
                adapter.refresh(it)
            }
            recycler_view.setNoMore(it.size <= 10)
            ll_empty?.visibility = if (adapter.itemCount == 0) View.VISIBLE else View.GONE
        } ?: let {
            recycler_view.setNoMore(true)
            if (isLoadMore) {
                ll_empty.gone()
                recycler_view.loadMoreComplete()
            } else {
                recycler_view.refreshComplete()
                adapter.clear()
                ll_empty.show()
            }
        }
    }

    override fun onFetchError() {
        if (isLoadMore) {
            page--
            recycler_view.loadMoreComplete()
        } else
            recycler_view.refreshComplete()
        if (adapter.itemCount > 0) ll_empty.gone() else ll_empty.show()
    }
}
